app.controller('AgencyOrderCtrl', function ($scope, ajax, $modal, toaster, $filter, $rootScope, authorities) {

    $scope.agencyList = [];
    $scope.queryMap = {};
    $scope.timeRange = {};
    $scope.flag = false;

    $scope.reload = $scope.page.loader(
        'agency/{userId}/{pageIndex}/{pageSize}/summary'.format({ userId: $rootScope.loginUser.id }));
    $scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
    $scope.reload(1);

    $scope.query = function () {
        if ($scope.agencyList.selected != undefined) {
            $scope.queryMap.agencyId = $scope.agencyList.selected.id;
        } else if ($scope.agencyList.selected == undefined) {
            delete $scope.queryMap.agencyId;
        }
        if ($scope.statusList.selected != undefined) {
            $scope.queryMap.status = $scope.statusList.selected.id;
        } else if ($scope.statusList.selected == undefined) {
            delete $scope.queryMap.status;
        }
        if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
            var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
            var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
            $scope.queryMap.createTime = starTime + "#@" + endTime;
        } else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
            delete $scope.queryMap.createTime;
        }

        $scope.reload(1, $scope.queryMap);
    };

    $scope.total = function () {
        if ($scope.agencyList.selected != undefined) {
            $scope.queryMap.agencyId = $scope.agencyList.selected.id;
        } else if ($scope.agencyList.selected == undefined) {
            delete $scope.queryMap.agencyId;
        }
        if ($scope.statusList.selected != undefined) {
            $scope.queryMap.status = $scope.statusList.selected.id;
        } else if ($scope.statusList.selected == undefined) {
            delete $scope.queryMap.status;
        }
        if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
            var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
            var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
            $scope.queryMap.createTime = starTime + "#@" + endTime;
        } else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
            delete $scope.queryMap.createTime;
        }
        ajax.call(new Req.GET({
            params: $scope.queryMap,
            resource: 'agency/{userId}/total'.format({ userId: $rootScope.loginUser.id })
        })).then(function (result) {
            $scope.summary = result.result;
            $scope.agencyName = $scope.summary[0];//代售点名称
            $scope.totalCount = $scope.summary[1];//订单总数
            $scope.orderTotal = $scope.summary[2];//订单总金额
            $scope.orderPaymentTotal = $scope.summary[3];//订单应付总金额
            $scope.orderRefundToatal = $scope.summary[4];//退款总金额
            $scope.insuranceTotal = $scope.summary[5];//保险总金额
            $scope.ticketCount = $scope.summary[6];//车票总数
            $scope.imOrderPaymentTotal = $scope.summary[7];//电子客票应付总金额
            $scope.imOrderRefundToatal = $scope.summary[8];//电子客票退款总金额
            $scope.paymentCount = $scope.summary[9];//支付流水数
            $scope.priceTotal = $scope.summary[10];//总收入
            $scope.flag = true;
        }, function () { });
    };

    //订单状态
    $scope.statusList = [
        { id: 1, name: "待付款" },
        { id: 2, name: "支付中" },
        { id: 3, name: "已支付" },
        { id: 4, name: "已完成" },
        { id: 5, name: "已取消" },
        { id: 6, name: "订单超时" },
        { id: 7, name: "退款中" },
        { id: 8, name: "退款完成" },
        { id: 9, name: "退款失败" }
    ];
    if (authorities.hasAuthority('BOOKING')) {
        ajax.get('agency/list', function (result) {
            if (result && result.code == 200) $scope.agencyList = result.result;
        }, function () { });
    }
});
